package com.tiansheng.controller;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.tiansheng.model.entity.Account;
import com.tiansheng.model.entity.AccountDao;
import com.tiansheng.model.entity.Settings;
import com.tiansheng.util.HibernateUtil;
import com.tiansheng.util.TimeUtil;

public class Test1 {
	public static void main(String[] args) {
//		System.out.println(TimeUtil.getFormatTime(new Date()));
		Test1.getTest();
	}
	
	public static void getTest(){
		Session s = HibernateUtil.currentSession();
		
		
		try{
			s.beginTransaction();
			String hql = "from Settings";
			Query q = s.createQuery(hql);
			List list = q.list();
			StringBuffer sb = new StringBuffer();
			for(Settings a:(List<Settings>) list){
				sb.append(a.getSettingsId()+":");
				sb.append(a.getSettingsType()+":");
				sb.append(a.getSettingsValue()+"\n");
				
			}
			System.out.println(sb);
			s.getTransaction().commit();
		}catch(HibernateException e){
			System.out.println(e.getMessage());
			e.printStackTrace();
			
		}finally{
			HibernateUtil.closeSession(s);
		}
		
	}
	
	public static void setTest(){
		Session s = HibernateUtil.currentSession();
		
		
		try{
			s.beginTransaction();
			s.save(new Settings(4,"yajun"));
			s.getTransaction().commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(s);
		}
		
	}
	
	public static void accountTest(){
		
		Account a = null;
		try {
			a = AccountDao.getEntityById(4);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return;
		}
		if(a!=null){
			System.out.println(a.getAccountName());
		}
		
		
	}

}
